package com.yoomiito.app.gift;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.yoomiito.app.R;
import com.yoomiito.app.base.App;
import com.yoomiito.app.model.gift.GiftGoods;
import com.yoomiito.app.ui.YouPinGoodsActivity;
import com.yoomiito.app.ui.gift.GiftActivity;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.ImageLoader;
import com.yoomiito.app.utils.SpannableUtils;
import com.yoomiito.app.utils.UserInfoConfig;
import com.yoomiito.app.widget.CouponView;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

public class NewGiftAdapter extends BaseQuickAdapter<GiftGoods, BaseViewHolder> {
    //用于区分不同的页面  目前在优品 和 礼包页面使用了GiftAdapter
    //不同的页面 佣金 奖 券 等的显示规则不同 用mPageType来进行区分
    private String mPageType;

//    public YouPinAdapter(@Nullable List<GiftGoods> data) {
//        super(R.layout.item_gife_goods, data);
//    }

    public NewGiftAdapter(@Nullable List<GiftGoods> data, String PageType) {
        super(R.layout.item_gife_goods_1, data);
        this.mPageType = PageType;
    }

    @Override
    protected void convert(BaseViewHolder helper, GiftGoods item) {
        if (GiftActivity.GiftFragment.equals(mPageType)) {
            View view = helper.getView(R.id.item_gift_goods_v);
            RecyclerView.LayoutParams lp = (RecyclerView.LayoutParams) view.getLayoutParams();
            lp.leftMargin = DensityUtil.dp2px(12);
            lp.rightMargin = DensityUtil.dp2px(12);
            view.setLayoutParams(lp);
            helper.setVisible(R.id.couponView,item.getYo_money()==0?false:true);
            CouponView songView = helper.getView(R.id.couponView);
            songView.setHeaderText("送");
            songView.setText(item.getYo_money()+"优米");
        }
        //product_type 0-表示正常VIP产品，1-表示3650Vip产品,2-爆品',
        ImageView goodsIv = helper.getView(R.id.item_gift_pic);
        ImageView iv = helper.getView(R.id.goodsFlag);
        TextView titleTv = helper.getView(R.id.goodsTitle);
        CouponView awardView = helper.getView(R.id.awardView);


        TagFlowLayout tagFlowLayout = helper.getView(R.id.item_gift_flowLayout);
        List<String> s = new ArrayList<>();
        if (!TextUtils.isEmpty(item.getProduct_point())) {
            s.add(item.getProduct_point());
        }
//        s.add("三年变得十分难堪的事烦恼");
//        s.add("法大师傅士大夫");


        tagFlowLayout.setAdapter(new TagAdapter<String>(s) {
            @Override
            public View getView(FlowLayout parent, int position, String o) {
                TextView tv = (TextView) LayoutInflater.from(tagFlowLayout.getContext()).inflate(R.layout.item_goods_flag, parent, false);
                tv.setText(o.length()>8?(o.substring(0,8)+"..."):o);
                return tv;
            }
        });
        TextView priceTv = helper.getView(R.id.item_gift_goods_price);
//        TextView oldPriceTv = helper.getView(R.id.item_gift_goods_old_price);
        TextView saleSizeTv = helper.getView(R.id.item_gift_goods_sale_size);

        ImageLoader.getInstance().into(goodsIv.getContext(), item.getBanner(), goodsIv, R.drawable.default_long_pic);

        helper.setText(R.id.goodscontent,item.productPoint);

        //大礼包标记
        if (YouPinGoodsActivity.YouPinGoodsAct.equals(mPageType)) {
            iv.setVisibility(View.GONE);
        } else {
            iv.setVisibility((0 == item.getProduct_type()) ? View.GONE : View.VISIBLE);
        }

        titleTv.setText(item.getProduct_name());

        //奖标记
        if (YouPinGoodsActivity.YouPinGoodsAct.equals(mPageType)) {//优品 爆品
            if (SpannableUtils.isEmptyOr0(item.getCommission())) {
                awardView.setVisibility(View.GONE);
            } else {//粉丝以上显示奖
                awardView.setVisibility(UserInfoConfig.getRoleId() > 0 ? View.VISIBLE : View.GONE);
                awardView.setText("¥" + SpannableUtils.handlePrice(item.getCommission()));
            }
        } else {
            if (App.mAppConfig.getIsLogin()){
                if (SpannableUtils.isEmptyOr0(item.getCommission())) {
                    awardView.setVisibility(View.GONE);
                } else {//vip以上显示奖
                    awardView.setVisibility(UserInfoConfig.getRoleId() > 7 ? View.VISIBLE : View.GONE);
                    awardView.setText("¥" + SpannableUtils.handlePrice(item.getCommission()));
                }
            }else {
                awardView.setVisibility(View.GONE);
            }
        }

        priceTv.setText(SpannableUtils.getSmallMoneyFlag("¥" + SpannableUtils.handlePrice(item.getSale_price())));
//        oldPriceTv.setText(SpannableUtils.isEmptyOr0(item.getMarket_price()) ? "专享价" : (SpannableUtils.getSpannable("¥" + SpannableUtils.handlePrice(item.getMarket_price()), 0)));
        saleSizeTv.setText("已售" + item.getVolume());
    }
}
